package com.demo2do.boyle.client.entity.Advertisement;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.demo2do.boyle.common.entity.aduser.material.MaterialImage;
import com.demo2do.boyle.common.entity.aduser.material.MaterialKeywords;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.core.utils.DateUtils;

public class AdvertisementDto {
	
	private Integer id;
	
	private String adTile;
	
	private String cover;
	
	private Integer reward;
	
	private int restDay;
	
	private int restHour;
	
	private String socialTypeName;
	
	private List<String> materialKeywords;
	
	private String description;
	
	private List<String> materialImages;
	
	private String link;
	
	/**
	 * The default construct
	 */
	public AdvertisementDto() {
		
	}
	
	/**
	 * The construct
	 * 
	 * @param ad
	 */
	public AdvertisementDto(Advertisement ad) {
		this.id = ad.getId();
		this.adTile = ad.getAdTitle();
		this.cover = ad.getMaterial().getBrandImage();
		this.reward = 10;
		
		this.socialTypeName = ad.getSocialType().getDescription();
		
		List<String> materialKeywords = new ArrayList<String>();
		for(MaterialKeywords materialKeyword : ad.getMaterial().getMaterialKeywords()) {
			materialKeywords.add(materialKeyword.getKeywords());
		}
		this.materialKeywords = materialKeywords;
		
		this.description = ad.getMaterial().getDescription();
		
		List<String> materialImages = new ArrayList<String>();
		for(MaterialImage materialImage : ad.getMaterial().getMaterialImages()) {
			materialImages.add(materialImage.getImage());
		}
		
		this.materialImages = materialImages;
		
		this.link = ad.getMaterial().getLink();
		
		this.initRestTime(ad);
	}
	
	public AdvertisementDto initRestTime(Advertisement ad) {
		//update by Jason.Y at 2015/12/10 ( this code need to change back !)
		//int day = DateUtils.getIntervalDays(new Date(), ad.getEndDate()).size();
		int day = DateUtils.getIntervalDays(ad.getStartDate(), ad.getEndDate()).size();
		
		if(day == 0) {
			this.restDay = 0;
			long intertime = ad.getEndDate().getTime()-new Date().getTime();
			long interday=intertime/(24*60*60*1000);
			int hour = Integer.parseInt(String.valueOf(intertime/(60*60*1000)-interday*24));		 
			if(hour == 0) {
				this.restHour = 0;
			} else {
				this.restHour = hour;
			}
		}else {
			this.restDay = day;
		}
		return this;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @return the adTile
	 */
	public String getAdTile() {
		return adTile;
	}

	/**
	 * @return the cover
	 */
	public String getCover() {
		return cover;
	}

	/**
	 * @return the reward
	 */
	public Integer getReward() {
		return reward;
	}

	/**
	 * @return the restDay
	 */
	public int getRestDay() {
		return restDay;
	}

	/**
	 * @return the socialTypeName
	 */
	public String getSocialTypeName() {
		return socialTypeName;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the materialImages
	 */
	public List<String> getMaterialImages() {
		return materialImages;
	}
	
	/**
	 * @return the materialKeywords
	 */
	public List<String> getMaterialKeywords() {
		return materialKeywords;
	}

	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @param adTile the adTile to set
	 */
	public void setAdTile(String adTile) {
		this.adTile = adTile;
	}

	/**
	 * @param cover the cover to set
	 */
	public void setCover(String cover) {
		this.cover = cover;
	}

	/**
	 * @param reward the reward to set
	 */
	public void setReward(Integer reward) {
		this.reward = reward;
	}

	/**
	 * @param restDay the restDay to set
	 */
	public void setRestDay(int restDay) {
		this.restDay = restDay;
	}

	/**
	 * @param socialTypeName the socialTypeName to set
	 */
	public void setSocialTypeName(String socialTypeName) {
		this.socialTypeName = socialTypeName;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param materialKeywords the materialKeywords to set
	 */
	public void setMaterialKeywords(List<String> materialKeywords) {
		this.materialKeywords = materialKeywords;
	}
	
	/**
	 * @param materialImages the materialImages to set
	 */
	public void setMaterialImages(List<String> materialImages) {
		this.materialImages = materialImages;
	}

	/**
	 * @param link the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * @return the restHour
	 */
	public int getRestHour() {
		return restHour;
	}

	/**
	 * @param restHour the restHour to set
	 */
	public void setRestHour(int restHour) {
		this.restHour = restHour;
	}

}
